Python supports an interesting syntax that lets you define one-line mini-functions using a construct called lambda on the fly. These functions can be anonymous functions (i.e. functions that are not bound to a name) at runtime. It is often used in conjunction with typical functional concepts like filter(), map() and reduce().

Creating a simple lambda function.


In [ ]:
l_func = lambda x: x**2
print (l_func(4))

As you can see that l_func function return the same as:


def foo(x):
    return x**2
but it does not contain a return statement, instead it always contains an expression which is returned.

You can put a lambda definition anywhere a function is expected, and you don't have to assign it to a variable at all. For example:


In [ ]:
def power_generator(n):
    return lambda x: x**n

func1 = power_generator(2)
func2 = power_generator(3)

print ('func1(2) = ', func1(2), 'func2(3) = ', func2(3))
print (power_generator(5)(2))

In the previous example, power_generator(n) generates a function on the fly and returns it. So func1 is nothing but a function returning $x^2$ and so is func2. In the last example power_generator(5)(2) demonstrates that you don't have to assign the function anywhere. power_generator(5) would create a function returning $x^5$ which would take the input parameter (2) and return 32.

map

map(function, iterable,...) apply function to every item of iterable and returns a list of results. This can be used with lambda function.


In [ ]:
print (list(map(lambda x: x**2, range(3))))

#taking one step further
print (list(map(power_generator(4), range(3))))

filter

Moving further, the next application of lambda function is filter function. filter(function, iterable) constructs a list from those elements of iterable for which function returns true. Iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
filter(function, iterable) is equivalent to [item for item in iterable if function(item)].
For example


In [ ]:
print (list(filter(lambda x: x%3 == 0, range(30))))

In the above example filter returned a list of all elements for which x%3 == 0 was True. So the list contained [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]

sorted

The next function, sorted is used for sorting a iterable.
sorted(iterable[, cmp[, key[, reverse]]]) returns a new sorted list from the items of a iterable.
key specifies a function of one argument that is used to extract a comparison key from each list element.
reverse reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.


In [ ]:
#sorting the list of strings according to length of strings
string_list = "Hello welcome you all to python workshop".split()
print (sorted(string_list, key=lambda x: len(x)))

The above code sorted the list according to the key i.e. len(string).
The following the example illustrating the cmp argument: